package com.huilian.iotbox.admin.wap.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.wap.dao.NoticeDao;
import com.huilian.iotbox.admin.wap.service.NoticeService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.NoticeDto;
import com.huilian.iotbox.data.enums.NoticeStatusEnum;
import com.huilian.iotbox.data.po.Notice;
import com.huilian.iotbox.data.vo.NoticeVo;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 谢洛涛
 * @date 2021/10/9 16:22
 */
@Service
public class NoticeServiceImpl implements NoticeService {
    @Resource
    private NoticeDao noticeDao;

    @Override
    public PageInfo findNoticeListByUserId(UserInfo userInfo, NoticeDto noticeDto) {
        PageHelper.startPage(noticeDto.getPageNum(), noticeDto.getPageSize());
        List<NoticeVo> list = noticeDao.findNoticeListByUserId(userInfo);
        // 将查询出来的数据改成正常状态
        for (NoticeVo noticeVo : list) {
            if (NoticeStatusEnum.UNREAD.getCode().equals(noticeVo.getStatus())) {
                Notice notice = new Notice();
                notice.setId(noticeVo.getId());
                notice.setStatus(NoticeStatusEnum.NORMAL.getCode());
                noticeDao.update(notice);
            }
        }
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public Integer findDidNotReadCount(UserInfo userInfo) {
        return noticeDao.findDidNotReadCount(userInfo);
    }

    @Override
    public NoticeVo findNotice(UserInfo userInfo) {
        return noticeDao.findNotice(userInfo);
    }

    @Override
    public ServerResponse changeNormal(Notice notice) {
        notice.setStatus(NoticeStatusEnum.NORMAL.getCode());
        int i = noticeDao.update(notice);
        if (i <= 0) {
            return ServerResponse.createByErrorMessage("更新数据库失败！");
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public int update(Notice notice) {
        return noticeDao.update(notice);
    }
}
